﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiliBiliCSharpStudy.old {
    class LoopWhileDemo {
        public static void demo() {
            //test1();
            test2();
        }

        // 使用 do while ； 并且校验输入的是否是数字；程序更健壮！
        public static void test2() {
            int score = 0;
            int sum = 0;
            do {
                Console.WriteLine("Please input first number:");
                string str1 = Console.ReadLine();

                //检验到“end”时退出游戏
                if (str1.ToLower() == "end") {
                    break;
                }

                int x = 0;
                try {
                    x = int.Parse(str1);
                } catch {
                    Console.WriteLine("First number has problem! Restart");
                    continue;
                }

                Console.WriteLine("Please input seconde number:");
                string str2 = Console.ReadLine();

                //检验到“end”时退出游戏
                if (str2.ToLower() == "end") {
                    break;
                }

                int y = 0;
                try {
                    y = int.Parse(str2);
                } catch {
                    Console.WriteLine("Second number has problem! Restart");
                    continue;
                }

                sum = x + y;
                if (sum == 100) {
                    score++;
                    Console.WriteLine("Correct! {0}+{1}={2}", x, y, sum);
                } else {
                    Console.WriteLine("Error! {0}+{1}={2}", x, y, sum);
                }
            } while (sum == 100);
            Console.WriteLine("your score is {0}", score);
            Console.WriteLine("Game Over");
        }


        public static void test1() {
            int score = 0;
            bool canContinue = true;
            while (canContinue) {
                Console.WriteLine("Please input first number:");
                string str1 = Console.ReadLine();
                int x = int.Parse(str1);

                Console.WriteLine("Please input seconde number:");
                string str2 = Console.ReadLine();
                int y = int.Parse(str2);

                int sum = x + y;
                if (sum == 100) {
                    score++;
                    Console.WriteLine("Correct! {0}+{1}={2}", x, y, sum);
                } else {
                    Console.WriteLine("Error! {0}+{1}={2}", x, y, sum);
                    canContinue = false;
                }
            }
            Console.WriteLine("your score is {0}", score);
            Console.WriteLine("Game Over");
        }
    }
}
